import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loading: 0, //加载转圈效果
    //token相关
    token: localStorage.getItem("token"),
    userInfo:JSON.parse(localStorage.getItem("userInfo")),
    courseId:sessionStorage.getItem("courseId"),
    //登录标记
    loginFlag: localStorage.getItem("login")
  },
  getters: {
    //loading
    GET_LOADING (state) { return state.loading },
    //token相关
    getUser: state=>{
      return state.userInfo;
    },
    getToken:state=>{
      if(state.token==null){
        return '';
      }
      else{
        return state.token;
      }
    },
    getCourseId:state=>{
      return this.state.courseId
    },
    //是否登录标记
    loginFlag:state=>{
      return state.loginFlag
    }
  },
  mutations: {
    //loading
    SET_LOADING (state, loading) { state.loading = loading },
    //token相关
    SET_TOKEN:(state,token)=>{
      state.token=token;
      localStorage.setItem("token",token);
    },
    SET_USERINFO:(state,userInfo)=>{
      state.userInfo=userInfo;
      localStorage.setItem("userInfo",JSON.stringify(userInfo));
    },
    REMOVE_INFO:(state)=>{
      state.token='',
          state.userInfo=[];
      localStorage.setItem("token",'');
      localStorage.setItem("userInfo",JSON.stringify(''));
    },
    //是否登录过
    IS_LOGIN:(state,loginFlag)=>{
      state.loginFlag=loginFlag;
      localStorage.setItem("login",loginFlag)
    }
  },
  actions: {
  },
  modules: {
  }
})
